<div class="yiiForm">

<p>
Fields with <span class="required">*</span> are required.
</p>

<?php echo CHtml::beginForm(); ?>

<?php echo CHtml::errorSummary($model); ?>

<div class="simple">
<?php echo CHtml::activeLabelEx($model,'name'); ?>
<?php echo CHtml::activeTextField($model,'name',array('size'=>40,'maxlength'=>40)); ?>
</div>

<?php 
$selected_services = array(); 
foreach($model->services as $service) array_push($selected_services, $service->id);
?>
<div class="complex">
	<span>
		<?php echo CHtml::label($model->getAttributeLabel('services'),'Services'); ?>
	</span>
	<div>
		<?php echo CHtml::checkBoxList('Services', $selected_services, CHtml::listData(Service::model()->findAll(),'id','name'));  ?>
	</div>
</div>

<div class="simple" style="padding-left: 110px;">
<table class="dataGrid">
	<caption><?php echo CHtml::encode($model->getAttributeLabel('libraryBooks')); ?></caption>
	<thead>
		<tr>
			<th><?php echo CHtml::encode(LibraryBook::model()->getAttributeLabel('bookISBN')); ?>
			</th>
			<th><?php echo CHtml::encode(LibraryBook::model()->getAttributeLabel('location')); ?>
			</th>
			<th>Actions
			</th>
		</tr>
	</thead>
	<tbody>
	
	<?php if (is_array($model->libraryBooks)) {  ?>
	<?php  foreach ($model->libraryBooks as $idx=>$libraryBook) { ?>
		<tr>
			<td>
			<?php 
			$this->widget('CAutoComplete',
				array(
					'model'=>$libraryBook,
					'attribute'=>"bookISBN[$idx]",
					'url'=>array('book/bookLookup'),
					'htmlOptions'=>array('size'=>22,'maxlength'=>22),
					'methodChain'=>'.result(function(event,item){$(this).val(item[1]);})'
				)
			); 
			?>
			<?php echo CHtml::error($libraryBook,"bookISBN"); ?></td>
			<td><?php echo CHtml::activeTextField($libraryBook,"location[$idx]",array('size'=>22,'maxlength'=>22)); ?>
			<?php echo CHtml::error($libraryBook,"location"); ?></td>
			<td><?php echo CHtml::linkButton('Delete',array(
		      	  'submit'=>'',
		      	  'params'=>array('deletebook'=>'1','idx'=>$idx),
		      	  'confirm'=>"Do you really want to delete this book ?")); ?>
			</td>
		</tr>
		<?php } ?>
		<?php } ?>
	</tbody>
</table>
</div>

<div class="action">
	<?php echo CHtml::submitButton('Add a book',array('name'=>'addbook')); ?>
</div>

<div class="simple" style="padding-left: 110px;">
<table class="dataGrid">
	<caption><?php echo CHtml::encode($model->getAttributeLabel('libraryComments')); ?></caption>
	<thead>
		<tr>
			<th><?php echo CHtml::encode(LibraryBook::model()->getAttributeLabel('comment')); ?>
			</th>
			<th>Actions
			</th>
		</tr>
	</thead>
	<tbody>
	
	<?php if (is_array($model->libraryComments)) {  ?>
	<?php  foreach ($model->libraryComments as $idx=>$libraryComment) { ?>
		<tr>
			<td><?php echo CHtml::activeTextArea($libraryComment,"comment[$idx]"); ?>
			<?php echo CHtml::error($libraryComment,"comment"); ?></td>
			<td><?php echo CHtml::linkButton('Delete',array(
		      	  'submit'=>'',
		      	  'params'=>array('deletecomment'=>'1','idx'=>$idx),
		      	  'confirm'=>"Do you really want to delete this comment ?")); ?>
			</td>
		</tr>
		<?php } ?>
		<?php } ?>
	</tbody>
</table>
</div>

<div class="action">
	<?php echo CHtml::submitButton('Add a comment',array('name'=>'addcomment')); ?>
</div>

<div class="action">
	<?php echo CHtml::submitButton($update ? 'Save' : 'Create'); ?>
</div>

<?php echo CHtml::endForm(); ?>

</div><!-- yiiForm -->